package com.hu.common.util.adm;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.LongSerializationPolicy;

import java.lang.reflect.Type;

/**
 * @author 355760
 * @ClassName ObjectHelper
 * @Description 对象解析类
 */
public class ObjectParser {

    @SuppressWarnings("unchecked")
    public static <T> T parse (Object object, Class<?> t) {
        final Gson gson = new Gson();
        final String s = gson.toJson(object);
        return (T) gson.fromJson(s, t);
    }

    @SuppressWarnings("unchecked")
    public static <T> T parse (String str, Class<?> t) {
        final Gson gson = new Gson();
        return (T) gson.fromJson(str, t);
    }

    public static <T> T parseComplexObj (String json, Type type) {
        return new Gson().fromJson(json, type);
    }

   public static String toJson(Object object) {
       GsonBuilder gsonBuilder = new GsonBuilder();
       gsonBuilder.setLongSerializationPolicy( LongSerializationPolicy.STRING );
       Gson gson = gsonBuilder.create();
       return gson.toJson(object);
   }
}